package io.renren.modules.pay;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.constant.Dict;
import io.renren.common.entity.pay.SysDeptEntity;
import io.renren.common.redis.RedisService;
import io.renren.modules.sys.dao.SysDeptDao;
import io.renren.modules.sys.dao.SysParamsDao;
import io.renren.modules.sys.entity.SysParamsEntity;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/*
 *初始化支付相关缓存
 */
@Component
@Order(value = 1)
@Data
public class InitCache implements CommandLineRunner {
	@Autowired
	private RedisService redisService;
	@Autowired
	private SysDeptDao sysDeptDao;

	@Autowired
	private SysParamsDao sysParamsDao;


	@Override
	public void run(String... args) throws Exception {
		this.initCache();
	}

	public void initCache() {
		//缓存部门
		List<SysDeptEntity> deptlist = sysDeptDao.selectList(new QueryWrapper<SysDeptEntity>().eq("del_flag", "0"));
		for (SysDeptEntity entity : deptlist) {
			redisService.setHashMap(Dict.MERCHANT, entity.getId(), JSON.toJSONString(entity));
		}
		//缓存系统参数
		List<SysParamsEntity> sysParamslist = sysParamsDao.selectList(new QueryWrapper<SysParamsEntity>());
		for (SysParamsEntity entity : sysParamslist) {
			redisService.setHashMap(Dict.SYSCONFIG, entity.getParamCode(), JSON.toJSONString(entity));
		}
	}

}
